<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/vhuichen.ico">
  <link rel="mask-icon" href="/vhuichen.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"vhuichen.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"display","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="基本类型数据去重（4种方法）1、利用 NSDictionary key 的唯一性2、利用 NSSet 的特性，数据不能重复3、利用 NSArray 的 containsObject 方法4、利用 NSArray 的 valueForKeyPath 方法，传入的 keyPath 为 @distinctUnionOfObjects.self 示例1234567891011121314151617181">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS 数据去重的几种方法">
<meta property="og:url" content="https://vhuichen.github.io/2018/08/2018/08/iOS%20%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95/index.html">
<meta property="og:site_name" content="vhuichen">
<meta property="og:description" content="基本类型数据去重（4种方法）1、利用 NSDictionary key 的唯一性2、利用 NSSet 的特性，数据不能重复3、利用 NSArray 的 containsObject 方法4、利用 NSArray 的 valueForKeyPath 方法，传入的 keyPath 为 @distinctUnionOfObjects.self 示例1234567891011121314151617181">
<meta property="og:locale">
<meta property="article:published_time" content="2018-08-03T16:00:00.000Z">
<meta property="article:modified_time" content="2018-08-12T12:00:32.178Z">
<meta property="article:author" content="vhuichen">
<meta property="article:tag" content="数据去重">
<meta property="article:tag" content="hash">
<meta property="article:tag" content="isEqual">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://vhuichen.github.io/2018/08/2018/08/iOS%20%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://vhuichen.github.io/2018/08/2018/08/iOS%20%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95/","path":"2018/08/2018/08/iOS 数据去重的几种方法/","title":"iOS 数据去重的几种方法"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>iOS 数据去重的几种方法 | vhuichen</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">vhuichen</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录、分享</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section">Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section">About</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D%EF%BC%884%E7%A7%8D%E6%96%B9%E6%B3%95%EF%BC%89"><span class="nav-number">1.</span> <span class="nav-text">基本类型数据去重（4种方法）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B"><span class="nav-number">1.1.</span> <span class="nav-text">示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D"><span class="nav-number">2.</span> <span class="nav-text">自定义类型数据去重</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A4%BA%E4%BE%8B-1"><span class="nav-number">2.1.</span> <span class="nav-text">示例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F"><span class="nav-number">2.2.</span> <span class="nav-text">注意</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="vhuichen"
      src="/images/vhuichen.jpg">
  <p class="site-author-name" itemprop="name">vhuichen</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://vhuichen.github.io/2018/08/2018/08/iOS%20%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/vhuichen.jpg">
      <meta itemprop="name" content="vhuichen">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vhuichen">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="iOS 数据去重的几种方法 | vhuichen">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          iOS 数据去重的几种方法
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2018-08-04 00:00:00" itemprop="dateCreated datePublished" datetime="2018-08-04T00:00:00+08:00">2018-08-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2018-08-12 20:00:32" itemprop="dateModified" datetime="2018-08-12T20:00:32+08:00">2018-08-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/iOS%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">iOS基础</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="基本类型数据去重（4种方法）"><a href="#基本类型数据去重（4种方法）" class="headerlink" title="基本类型数据去重（4种方法）"></a>基本类型数据去重（4种方法）</h2><p>1、利用 NSDictionary key 的唯一性<br>2、利用 NSSet 的特性，数据不能重复<br>3、利用 NSArray 的 containsObject 方法<br>4、利用 NSArray 的 valueForKeyPath 方法，传入的 keyPath 为 <code>@distinctUnionOfObjects.self</code></p>
<h3 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h3><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSArray</span> *dataArray = @[<span class="string">@&quot;a&quot;</span>,<span class="string">@&quot;b&quot;</span>,<span class="string">@&quot;c&quot;</span>,<span class="string">@&quot;d&quot;</span>,<span class="string">@&quot;e&quot;</span>,<span class="string">@&quot;f&quot;</span>,<span class="string">@&quot;g&quot;</span>,</span><br><span class="line">                       <span class="string">@&quot;b&quot;</span>,<span class="string">@&quot;d&quot;</span>,<span class="string">@&quot;a&quot;</span>];</span><br><span class="line">                       </span><br><span class="line"><span class="built_in">NSMutableDictionary</span> *dict = [<span class="built_in">NSMutableDictionary</span> dictionary];</span><br><span class="line"><span class="keyword">for</span>(<span class="built_in">NSString</span> *str <span class="keyword">in</span> dataArray) &#123;</span><br><span class="line">    [dict setValue:str forKey:str];</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;dict = %@&quot;</span>,[dict allKeys]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSSet</span> *set = [<span class="built_in">NSSet</span> setWithArray:dataArray];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;set = %@&quot;</span>,[set allObjects]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSMutableArray</span> *array = [<span class="built_in">NSMutableArray</span> array];</span><br><span class="line"><span class="keyword">for</span> (<span class="built_in">NSString</span> *str <span class="keyword">in</span> dataArray) &#123;</span><br><span class="line">    <span class="keyword">if</span> (![array containsObject:str]) &#123;</span><br><span class="line">        [array addObject:str];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;array = %@&quot;</span>,array);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSArray</span> *keyPathArray = [dataArray valueForKeyPath:<span class="string">@&quot;@distinctUnionOfObjects.self&quot;</span>];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;keyPathArray = %@&quot;</span>,keyPathArray);</span><br></pre></td></tr></table></figure>
<p>通过上面4中方法均可过滤掉重复数据。</p>
<span id="more"></span>

<h2 id="自定义类型数据去重"><a href="#自定义类型数据去重" class="headerlink" title="自定义类型数据去重"></a>自定义类型数据去重</h2><p>可以通过重写 <code>hash</code> <code>isEqual</code> 两个方法实现  </p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//.h</span></span><br><span class="line"><span class="meta">#import <span class="string">&lt;Foundation/Foundation.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">VCHAnimal</span> : <span class="title">NSObject</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">assign</span>, <span class="keyword">nonatomic</span>) <span class="type">int</span> ID;</span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">assign</span>, <span class="keyword">nonatomic</span>) <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure>

<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//.m</span></span><br><span class="line"><span class="meta">#import <span class="string">&quot;VCHAnimal.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">VCHAnimal</span></span></span><br><span class="line"></span><br><span class="line">- (<span class="built_in">NSUInteger</span>)hash &#123;</span><br><span class="line">    <span class="keyword">return</span> [[<span class="built_in">NSString</span> stringWithFormat:<span class="string">@&quot;%zd&quot;</span>,<span class="keyword">self</span>.ID] hash];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="type">BOOL</span>)isEqual:(<span class="type">id</span>)object &#123;</span><br><span class="line">    <span class="keyword">if</span> ([object isKindOfClass:[<span class="keyword">self</span> <span class="keyword">class</span>]]) &#123;</span><br><span class="line">        <span class="keyword">return</span> ((VCHAnimal *)object).ID == <span class="keyword">self</span>.ID;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">NO</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure>

<h3 id="示例-1"><a href="#示例-1" class="headerlink" title="示例"></a>示例</h3><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSMutableArray</span> *array = [<span class="built_in">NSMutableArray</span> array];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">    VCHAnimal *animal = [VCHAnimal new];</span><br><span class="line">    animal.ID = i;</span><br><span class="line">    animal.age = arc4random()%<span class="number">1000</span>;</span><br><span class="line">    [array addObject:animal];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">    VCHAnimal *animal = [VCHAnimal new];</span><br><span class="line">    animal.ID = i;</span><br><span class="line">    animal.age = arc4random()%<span class="number">1000</span>;</span><br><span class="line">    [array addObject:animal];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSSet</span> *set = [<span class="built_in">NSSet</span> setWithArray:array];</span><br><span class="line"></span><br><span class="line">[array enumerateObjectsUsingBlock:^(VCHAnimal *obj, <span class="built_in">NSUInteger</span> idx, <span class="type">BOOL</span> * _Nonnull stop) &#123;</span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@&quot;obj = %@ ID = %zd&quot;</span>,obj, obj.ID);</span><br><span class="line">&#125;];</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@&quot;---------&quot;</span>);</span><br><span class="line">[set enumerateObjectsUsingBlock:^(VCHAnimal *obj, <span class="type">BOOL</span> * _Nonnull stop) &#123;</span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@&quot;obj = %@ ID = %zd&quot;</span>,obj, obj.ID);</span><br><span class="line">&#125;];</span><br><span class="line"></span><br><span class="line"><span class="comment">// log</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017af0&gt; ID = 0</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017b60&gt; ID = 1</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017b70&gt; ID = 2</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017b50&gt; ID = 0</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017b80&gt; ID = 1</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017b90&gt; ID = 2</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017ba0&gt; ID = 3</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000201e50&gt; ID = 4</span></span><br><span class="line"><span class="comment">// ---------</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017ba0&gt; ID = 3</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000201e50&gt; ID = 4</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017af0&gt; ID = 0</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017b60&gt; ID = 1</span></span><br><span class="line"><span class="comment">// obj = &lt;VCHAnimal: 0x600000017b70&gt; ID = 2</span></span><br></pre></td></tr></table></figure>

<p>用<code>containsObject </code>方法可以得到相同的效果。</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSMutableArray</span> *arraySort = [<span class="built_in">NSMutableArray</span> array];</span><br><span class="line"><span class="keyword">for</span> (VCHAnimal *animal <span class="keyword">in</span> array) &#123;</span><br><span class="line">    <span class="keyword">if</span> (![arraySort containsObject:animal]) &#123;</span><br><span class="line">        [arraySort addObject:animal];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><p>考虑到性能问题，hash 方法应该尽量简洁，避免做过多的运算。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D/" rel="tag"># 数据去重</a>
              <a href="/tags/hash/" rel="tag"># hash</a>
              <a href="/tags/isEqual/" rel="tag"># isEqual</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2018/07/2018/07/iOS%E5%BC%80%E5%8F%91%E4%B9%8B%20anchorPoint/" rel="prev" title="iOS开发之 anchorPoint">
                  <i class="fa fa-chevron-left"></i> iOS开发之 anchorPoint
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2019/05/2019/05/%E8%AE%B0%E4%B8%80%E6%AC%A1%E4%BB%A3%E7%A0%81%E4%BC%98%E5%8C%96/" rel="next" title="记一次代码优化">
                  记一次代码优化 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vhuichen</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
